export const HOOKS = [
  'beforeCreate',
  'created',
  'beforeMount',
  'mounted',
  'beforeUpdate',
  'updated',
  'beforeDestory',
  'destoryed'
]

let starts = {};

starts.data = function (parentVal, childVal) {
  return childVal;
}
// starts.computed = function () { }
// starts.watch = function () { }
// starts.methods = function () { }
HOOKS.forEach(hook => {
  starts[hook] = mergeHook;
})

function mergeHook(parentVal, childVal) {
  if (childVal) {
    if (parentVal) {
      // 有父亲，合并
      return parentVal.concat(childVal);
    } else {
      // 父亲没有值，返回新的数组
      return [childVal]
    }
  } else {
    return parentVal;
  }
}

export function mergeOptions(parent, child) {
  const options = {};
  // 如果有父亲，没有儿子
  for (let key in parent) {
    mergeField(key);
  }
  for (let key in child) {
    mergeField(key);
  }

  function mergeField(key) {
    if (starts[key]) { // {created:[createdA,createdB]}
      options[key] = starts[key](parent[key], child[key]);
    } else {
      options[key] = child[key] || parent[key];
    }
  }
  return options;
}
